package cn.net.withub.dataCollector.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class CommonUtils {
		
	
	public static boolean isNotEmpty(String ret){
		if(null==ret||ret.trim().equals("")){
			return false;
		}
		return true;
	}
	
	public static boolean isEmpty(String ret){
		if(null==ret||ret.trim().equals("")){
			return true;
		}
		return false;
	}
	
	/**
	 * 复制对象
	 * @param object 原对象
	 * @param paramType get set 首写字符大小写 1：大写  2:小写 默认大写 
	 * @return
	 * @throws Exception
	 */
	public static Object copy(Object object, Integer paramType) throws Exception {
		Class<?> classType = object.getClass();
		Object objectCopy = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
		Field fields[] = classType.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				String fieldName = field.getName();
				String firstLetter = fieldName.substring(0, 1).toUpperCase();
				if(paramType==2){
					firstLetter = fieldName.substring(0, 1).toLowerCase();
				}

				String getMethodName = "get" + firstLetter + fieldName.substring(1);
				String setMethodName = "set" + firstLetter + fieldName.substring(1);
				/*
				if(fieldName.equals("dtTimestamp")){
					getMethodName="getDtTimestamp";
					setMethodName="setDtTimestamp";
				}
				if(fieldName.equals("dtCzsj")){
					getMethodName="getDtCzsj";
					setMethodName="setDtCzsj";
				}
				*/
				if(fieldName.startsWith("dt")){
					getMethodName="getDt"+fieldName.replaceFirst("dt", "");
					setMethodName="setDt"+fieldName.replaceFirst("dt", "");
				}
				Method getMethod = classType.getMethod(getMethodName,new Class[] {});
				Method setMethod = classType.getMethod(setMethodName,new Class[] { field.getType() });
				Object value = getMethod.invoke(object, new Object[] {});
				setMethod.invoke(objectCopy, new Object[] { value });
			}
		return objectCopy;
	} 
	
	/**
	 * 比较法院级别
	 * @author LiuYang
	 * @param fydm 主法院代码
	 * @param fydm2 对比法院代码
	 * @return 0：同级 1：上级： 2：下级
	 * @throws Exception
	 */
	public static Integer compareFyjb(String fydm, String fydm2) throws Exception {
		Integer flag = 0;
		
		//同级的情况
		if((fydm.endsWith("00")||fydm.equals("VI0"))&&(fydm2.endsWith("00")||fydm2.equals("VI0"))){
			flag = 0;
		}
		else if((!fydm.substring(1, 2).equals("0")&&fydm.endsWith("0"))&&(!fydm2.substring(1,2).equals("0")&&fydm2.endsWith("0"))){
			flag = 0;
		}
		else if(fydm.indexOf("0")==-1&&fydm2.indexOf("0")==-1){
			flag = 0;
		}
		
		//上级的情况
		if((fydm.endsWith("00")||fydm.equals("VI0"))&&(!fydm2.endsWith("00")||!fydm2.equals("VI0"))){
			flag = 1;
		}
		else if((!fydm.substring(1, 2).equals("0")&&fydm.endsWith("0"))&&(!fydm2.substring(1,2).equals("0")&&!fydm2.endsWith("0"))){
			flag = 1;
		}

		//下级的情况
		if((!fydm.substring(1, 2).equals("0")&&fydm.endsWith("0"))&&(fydm2.endsWith("00")||fydm2.equals("VI0"))){
			flag = 2;
		}
		else if(fydm.indexOf("0")==-1&&fydm2.indexOf("0")>-1){
			flag = 2;
		}
		
		return flag;
	}
	
	/**
	 * 获取所有上级法院
	 * @author LiuYang
	 * @param fydm 主法院代码
	 * @return 多个法院以,间隔
	 * @throws Exception
	 */
	public static String getAllParentFydm(String fydm) throws Exception {
		String retFydm = "";
		
		if((fydm.endsWith("00")||fydm.equals("VI0"))){
			retFydm = "";
		}
		else if((!fydm.substring(1, 2).equals("0")&&fydm.endsWith("0"))){
			retFydm = fydm.substring(0,1)+"00";
			if(fydm.startsWith("VI")){
				retFydm = "VI0";
			}
		}
		else if(fydm.indexOf("0")==-1){
			retFydm = fydm.substring(0,1)+"00,"+fydm.substring(0,2)+"0";
			if(fydm.startsWith("VI")){
				retFydm = "VI0";
			}
		}
		
		return retFydm;
	}
	
	/**
	 * 获取上一级法院
	 * @author LiuYang
	 * @param fydm 主法院代码
	 * @return
	 * @throws Exception
	 */
	public static String getParentFydm(String fydm) throws Exception {
		String retFydm = "";
		
		if((fydm.endsWith("00")||fydm.equals("VI0"))){
			retFydm = "";
		}
		else if((!fydm.substring(1, 2).equals("0")&&fydm.endsWith("0"))){
			retFydm = fydm.substring(0,1)+"00";
			if(fydm.startsWith("VI")){
				retFydm = "VI0";
			}
		}
		else if(fydm.indexOf("0")==-1){
			retFydm = fydm.substring(0,2)+"0";
			if(fydm.startsWith("VI")){
				retFydm = "VI0";
			}
		}
		
		return retFydm;
	}
	
	public static String getRequestValue(String paras){
		return paras == null?"":paras;
	}
	
	public static Integer getFyjb(String fydm) {
		Integer fyjb = 4;
		//高院级别、新疆兵团法院特殊判断
		if(fydm.equals("000")){
			fyjb = 1;
		}else if (fydm.endsWith("00")||fydm.equals("VI0"))
			fyjb = 2;
		else if (fydm.endsWith("0"))
			fyjb = 3;
		else
			fyjb = 4;
		return fyjb;
	}

	public  static String getRemoteIpAddr(HttpServletRequest req) {
		String ip = null;
		try {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = req.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = req.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = req.getHeader("HTTP_CLIENT_IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = req.getHeader("HTTP_X_FORWARDED_FOR");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = req.getRemoteAddr();
			}
			if(ip!=null&&ip.equals("0:0:0:0:0:0:0:1")){
				return "127.0.0.1";
			}
			//ip = ip!=null&&ip.trim().indexOf(",")>0?(ip.split(",").length>1?ip.split(",")[1]:ip.split(",")[0]):ip;
			if(ip!=null&&ip.trim().split(",").length>1){
				String[] arrs = ip.trim().split(",");
				for(String ipAddress : arrs){
					if(ipAddress.startsWith("149.")){
						return ipAddress.trim();
					}
				}
			}
		} catch (Exception e) {
			ip = req.getRemoteAddr();
		}
		if(ip!=null&&-1==ip.indexOf(".")){
			return "127.0.0.1";
		}
		return ip;
	}

	/**
	 * 20160601 update 林老板那边要用不能用最高版本jdk特性
	 * @param str
	 * @return
	 */
	public static String convertSQLStr(String str){
		if(null == str) {
			str = "";
		}
		return "'"+str+"'";
	}

	public static String convertInStr(String str){
		if(null == str) {
			str = "";
		}
		String[] arr = str.split(",");
		StringBuffer sbf = new StringBuffer("");

		for (String s : arr) {
			sbf.append("'"+s+"',");
		}

		str = "".equals(sbf.toString())?"":sbf.toString().substring(0,sbf.toString().length()-1);
		return str;
	}

}
